package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

public class ViewCategoryServlet extends HttpServlet {

    private String categoryId;
    private static final String VIEW_CATEGORY = "/WEB-INF/jsp/catalog/Category.jsp";

    private LogService logService = new LogService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        categoryId = request.getParameter("categoryId");
        System.out.println("获取到的categoryid为"+categoryId);
        CatalogService service = new CatalogService();
        Category category = service.getCategory(categoryId);
        List<Product>productList = service.getProductListByCategory(categoryId);

        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("account");
        session.setAttribute("category",category);
        session.setAttribute("productList",productList);

        if(account != null){
            String logInfo = logService.logInfo(" ") + " 跳转到商品种类 " + category;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

        request.getRequestDispatcher(VIEW_CATEGORY).forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
